package com.kitty.game.pet.handler.upgrade;

import com.kitty.game.pet.model.Pet;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.pet.message.RespPetEnchantEnd;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import java.text.MessageFormat;

@Component
public class OpenEnchantPetHandler extends UpgradePetHandler {
    private static final String SUCCESS_MSG = "恭喜，你的#Y{0}#n成功开启点化，获得#R6000#n灵气，当前点化进度#R{1}%#n。";

    @Override
    public String acceptUpgradePet(Role role, Pet pet, String ids, String itemPos, String costType, String otherPetPos) {
        if (pet.getEnchant() == 2 || pet.getEnchant() == 1) {
            return MessageFormat.format(ERROR_OPER_MSG, "点化");
        }
        int limitNum = 0;//限制物品数量
        RoleEquip roleEquip = equipService.getRoleEquipByPos(role, Short.parseShort(itemPos));
        if (roleEquip == null) {
            return ERROR_ITEM;
        }
        if (!roleEquip.getName().equals("点化丹")) {
            return ERROR_ITEM;
        }
        if (roleEquip.isLimit()) {
            limitNum++;
        }
        petService.addLimitTime(pet, limitNum);
        equipService.delEquipNum(role, roleEquip, 1);
        int maxNimbus = getEnchantMaxNimbus(pet);
        String resultMessage;
        pet.setEnchant_k(maxNimbus);
        setEnchantValue(pet, 6000);
        if (maxNimbus < 6000) {
            pet.setEnchant((byte) 2);
            pet.setEnchant_v(0);
            pet.setEnchant_k(0);
            resultMessage =MessageFormat.format("恭喜，你的#Y{0}#n点化完成了。",pet.getName());
        } else {
            pet.setEnchant((byte) 1);
            pet.setEnchant_v(6000);
            double percent = (double) pet.getEnchant_v() / (double) pet.getEnchant_k();
            String percentMsg = "%.2f";
            percentMsg = String.format(percentMsg, (percent * 100));
            resultMessage =MessageFormat.format(SUCCESS_MSG, pet.getName(),percentMsg);
        }
        roleService.addAchieveScore(role,"201049");
        petService.refreshPetInfo(role, pet);
        refreshPetField(role,pet);
        RespPetEnchantEnd RespPetEnchantEnd = new RespPetEnchantEnd();
        MessagePusher.pushMessage(role, RespPetEnchantEnd);
        return resultMessage;
    }
}
